﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mitasoft.AdsShedule.Business;
using mitasoft.AdsShedule.Infrastructure;

namespace mitasoft.AdsShedule.Forms
{
    public partial class ManageClients : Infrastructure.BaseForm
    {
        #region fields
        private BindingSource bs;
        #endregion

        #region ctor
        public ManageClients()
        {
            InitializeComponent();
            this.Load += new EventHandler(ManageClients_Load);
            this.FormClosed += new FormClosedEventHandler(ManageClients_FormClosed);
        }
        #endregion

        #region override methods

        public override void LoadData()
        {
            bs = new BindingSource(BusinessAccess.Instance.DataSet, "Client");
        }
        public override void BindDataToControls()
        {
            dataGridClients.AutoGenerateColumns = false;
            dataGridClients.DataSource = bs;

            textBox1.DataBindings.Clear();
            textBox1.DataBindings.Add(new Binding("Text", this.bs, "Name"));
        }
        #endregion

        #region event handlers

        private void ManageClients_Load(object sender, EventArgs e)
        {
            AfterSave();
        }
        private void ManageClients_FormClosed(object sender, FormClosedEventArgs e)
        {
            BusinessAccess.Instance.SaveDataSet();
        }
        private void btnSalveaza_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                ErrorCheck.DoCheck(() =>
                {
                    (
                        (mitasoft.AdsShedule.Business.AdDS.ClientRow)
                        (((System.Data.DataRowView)(bs.Current)).Row)
                    ).Name = textBox1.Text;

                    Business.BusinessAccess.Instance.DataSet.Client.AddClientRow(
                        ((mitasoft.AdsShedule.Business.AdDS.ClientRow)
                        (((System.Data.DataRowView)(bs.Current)).Row))
                    );

                    var aa = Business.BusinessAccess.Instance.DataSet;
                });
            }
            else
            {
                bs.RemoveCurrent();
            }
            dataGridClients.Refresh();
            AfterSave();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            BeforeSave();

            ErrorCheck.DoCheck(() =>
            {
                bs.AddNew();
            });
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            ErrorCheck.DoCheck(() =>
            {
                bs.RemoveCurrent();
            });
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        #endregion

        private void BeforeSave()
        {
            textBox1.Enabled = true;
        }
        private void AfterSave()
        {
            textBox1.Enabled = false;
        }

    }
}

